﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Maticsoft.DBUtility;

namespace BBPA.DAL
{
   public class Group_Good
    {
        public Group_Good()
        { }
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ggid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Group_Good");
            strSql.Append(" where ggid=@ggid");
            SqlParameter[] parameters = {
					new SqlParameter("@ggid", SqlDbType.Int,4)
			};
            parameters[0].Value = ggid;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(BBPA.Model.Group_Good model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Group_Good(");
            strSql.Append("gg_gbid,gg_goodid,gggoodprice)");
            strSql.Append(" values (");
            strSql.Append("@gg_gbid,@gg_goodid,@gggoodprice)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@gg_gbid", SqlDbType.Int,4),
					new SqlParameter("@gg_goodid", SqlDbType.Int,4),
					new SqlParameter("@gggoodprice", SqlDbType.Money,8)};
            parameters[0].Value = model.gg_gbid;
            parameters[1].Value = model.gg_goodid;
            parameters[2].Value = model.gggoodprice;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(BBPA.Model.Group_Good model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Group_Good set ");
            strSql.Append("gg_gbid=@gg_gbid,");
            strSql.Append("gg_goodid=@gg_goodid,");
            strSql.Append("gggoodprice=@gggoodprice");
            strSql.Append(" where ggid=@ggid");
            SqlParameter[] parameters = {
					new SqlParameter("@gg_gbid", SqlDbType.Int,4),
					new SqlParameter("@gg_goodid", SqlDbType.Int,4),
					new SqlParameter("@gggoodprice", SqlDbType.Money,8),
					new SqlParameter("@ggid", SqlDbType.Int,4)};
            parameters[0].Value = model.gg_gbid;
            parameters[1].Value = model.gg_goodid;
            parameters[2].Value = model.gggoodprice;
            parameters[3].Value = model.ggid;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ggid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Group_Good ");
            strSql.Append(" where ggid=@ggid");
            SqlParameter[] parameters = {
					new SqlParameter("@ggid", SqlDbType.Int,4)
			};
            parameters[0].Value = ggid;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string ggidlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Group_Good ");
            strSql.Append(" where ggid in (" + ggidlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public BBPA.Model.Group_Good GetModel(int ggid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ggid,gg_gbid,gg_goodid,gggoodprice from Group_Good ");
            strSql.Append(" where ggid=@ggid");
            SqlParameter[] parameters = {
					new SqlParameter("@ggid", SqlDbType.Int,4)
			};
            parameters[0].Value = ggid;

            BBPA.Model.Group_Good model = new BBPA.Model.Group_Good();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public BBPA.Model.Group_Good DataRowToModel(DataRow row)
        {
            BBPA.Model.Group_Good model = new BBPA.Model.Group_Good();
            if (row != null)
            {
                if (row["ggid"] != null && row["ggid"].ToString() != "")
                {
                    model.ggid = int.Parse(row["ggid"].ToString());
                }
                if (row["gg_gbid"] != null && row["gg_gbid"].ToString() != "")
                {
                    model.gg_gbid = int.Parse(row["gg_gbid"].ToString());
                }
                if (row["gg_goodid"] != null && row["gg_goodid"].ToString() != "")
                {
                    model.gg_goodid = int.Parse(row["gg_goodid"].ToString());
                }
                if (row["gggoodprice"] != null && row["gggoodprice"].ToString() != "")
                {
                    model.gggoodprice = decimal.Parse(row["gggoodprice"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM V_Goupbuygood_good ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string column,string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  " + column);
            strSql.Append(" FROM V_Goupbuygood_good ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetGroupBuy_GoodList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM V_GroupBuy_GoodList ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" * ");
            strSql.Append(" FROM V_Goupbuygood_good ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM Group_Good ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.ggid desc");
            }
            strSql.Append(")AS Row, T.*  from Group_Good T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }
    }
}
